package main

import (
	"database/sql"
	"errors"
	_ "github.com/go-sql-driver/mysql"
	"log"
)

var user_db, _ = sql.Open("mysql", "mt11235:hwqmingdi@tcp(127.0.0.1:3306)/charity?charset=utf8")

func addUser(name string, password string) error {
	_, err := user_db.Exec(
		"insert into users (username, password) values (?, ?)",
		name,
		password)
	if err != nil {
		log.Println("error")
	}
	return err
}

// 登录时检验
func checkUser(name string, password string) (error, int) {
	row := user_db.QueryRow("select * from users where username=?", name)
	cname := ""
	cpasswd := ""
	err := row.Scan(&cname, &cpasswd)
	if err != nil {
		log.Println(err)
		return err, 0
	}
	if cpasswd != password {
		return errors.New("passwd wrong"), 1
	}
	return nil, 0
}
